package com.sh169.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class FreemarkerUtils {

	private static FreemarkerUtils freemarkerUtil;
	private static Configuration cfg;
	private static Map<String, Object> model ;
	
	private FreemarkerUtils(){}
	public static FreemarkerUtils getInstance(){
		if(freemarkerUtil ==null) {
			freemarkerUtil = new FreemarkerUtils();
			try {
				freemarkerUtil.init();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return freemarkerUtil;
	}
	private void init() throws Exception{
		try {
			cfg = new Configuration();
			cfg.setDirectoryForTemplateLoading(new File(ApplicationUtil.getWebHomePath()+"/"));
		} catch (IOException e) {
			throw new Exception("加载模板路径 失败",e);
		}
	}
	public static Map<String, Object> getInitMap(){
		
		if(model == null){
			model = new HashMap<String, Object>();
			model.put("contextPath","");
		}
		Map<String, Object> m = new HashMap<String, Object>();
		m.putAll(model);
		return m;
	}
	
	/**
	 * 为模板生成 String
	 * @param ftlPath
	 * @param model
	 * @throws ShopTemplateExcetion
	 */
	public String mergeTemplateIntoString(String ftlPath,Map<String,Object> model) throws Exception{
		if(ftlPath == null|| model ==null) {
			throw new Exception("param is null");
		}
		try {
			Template template = cfg.getTemplate(ftlPath,"utf-8");
			template.setEncoding("utf-8");
			StringWriter writer = new StringWriter();
			template.process(model, writer);
			return writer.toString();
		} catch (IOException e) {
			throw new Exception("io exception",e);
		} catch (TemplateException e) {
			throw new Exception("template exception",e);
		}
	}
	/**
	 * 生成静态文件
	 * @param ftlPath
	 * @param htmlPath
	 * @param model
	 * @throws ShopTemplateExcetion
	 */
	public void mergeTemplateIntoHtml(String ftlPath,String htmlPath,Map<String,Object> model) throws Exception {
		if(ftlPath == null|| model ==null) {
			throw new Exception("param is null");
		}
		String htmlString = ApplicationUtil.getWebHomePath()+htmlPath;
		String parentDir = htmlString.substring(0, htmlString.lastIndexOf("/")+1);
		File parentHtml = new File(parentDir);
		if(!parentHtml.exists()){
			parentHtml.mkdirs();
		}
		 try {
			OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(new File(htmlString)),"utf-8");
			writer.write(mergeTemplateIntoString(ftlPath, model));
			writer.flush();
			writer.close();
		 } catch (UnsupportedEncodingException e) {
			throw new Exception("html file UnsupportedEncoding",e);
		} catch (FileNotFoundException e) {
			throw new Exception("html file not found ",e);
		} catch (IOException e) {
			throw new Exception("create html io error ",e);
		}
	}
}
